<html>
<head><meta charset="utf-8"><title>How to determine if an inst has `nuw`/`nsw` attribute? · t-compiler/wg-llvm · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/index.html">t-compiler/wg-llvm</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/How.20to.20determine.20if.20an.20inst.20has.20.60nuw.60.2F.60nsw.60.20attribute.3F.html">How to determine if an inst has `nuw`/`nsw` attribute?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="205829499"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187780-t-compiler/wg-llvm/topic/How%20to%20determine%20if%20an%20inst%20has%20%60nuw%60/%60nsw%60%20attribute%3F/near/205829499" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/How.20to.20determine.20if.20an.20inst.20has.20.60nuw.60.2F.60nsw.60.20attribute.3F.html#205829499">(Aug 03 2020 at 18:26)</a>:</h4>
<p>Hi LLVM experts. I've been trying on my own to see if I can resolve <a href="https://bugs.llvm.org/show_bug.cgi?id=46943">https://bugs.llvm.org/show_bug.cgi?id=46943</a></p>



<a name="205829568"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187780-t-compiler/wg-llvm/topic/How%20to%20determine%20if%20an%20inst%20has%20%60nuw%60/%60nsw%60%20attribute%3F/near/205829568" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/How.20to.20determine.20if.20an.20inst.20has.20.60nuw.60.2F.60nsw.60.20attribute.3F.html#205829568">(Aug 03 2020 at 18:26)</a>:</h4>
<p>At this point I'm playing with this patch: <a href="https://gist.github.com/pnkfelix/6e8c3c4b990133864173e4ef0ad6bea8">https://gist.github.com/pnkfelix/6e8c3c4b990133864173e4ef0ad6bea8</a></p>



<a name="205829663"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187780-t-compiler/wg-llvm/topic/How%20to%20determine%20if%20an%20inst%20has%20%60nuw%60/%60nsw%60%20attribute%3F/near/205829663" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/How.20to.20determine.20if.20an.20inst.20has.20.60nuw.60.2F.60nsw.60.20attribute.3F.html#205829663">(Aug 03 2020 at 18:27)</a>:</h4>
<p>but its not working; I get this output during <code>opt -loop-reduce -debug</code>: </p>
<div class="codehilite"><pre><span></span><code>...
Use:   %5 = add nuw i8 %1, 0 is a non-overflowing binop
Use:   %1 = phi i8 [ 0, %0 ], [ %3, %while.cond ] is a non-overflowing binop
Use:   %1 = phi i8 [ 0, %0 ], [ %3, %while.cond ] is a non-overflowing binop
...
</code></pre></div>



<a name="205829708"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187780-t-compiler/wg-llvm/topic/How%20to%20determine%20if%20an%20inst%20has%20%60nuw%60/%60nsw%60%20attribute%3F/near/205829708" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/How.20to.20determine.20if.20an.20inst.20has.20.60nuw.60.2F.60nsw.60.20attribute.3F.html#205829708">(Aug 03 2020 at 18:27)</a>:</h4>
<p>so, what am I doing wrong in the code? At this point I'm assuming that there's something wrong with my understanding of how <code>dyn_cast</code> is supposed to work</p>



<a name="205829851"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187780-t-compiler/wg-llvm/topic/How%20to%20determine%20if%20an%20inst%20has%20%60nuw%60/%60nsw%60%20attribute%3F/near/205829851" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/How.20to.20determine.20if.20an.20inst.20has.20.60nuw.60.2F.60nsw.60.20attribute.3F.html#205829851">(Aug 03 2020 at 18:28)</a>:</h4>
<p>(okay, obviously I tweaked the debug output slightly since I did the run I pasted above; let me make a fresh run and edit the transcript)</p>



<a name="205830364"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187780-t-compiler/wg-llvm/topic/How%20to%20determine%20if%20an%20inst%20has%20%60nuw%60/%60nsw%60%20attribute%3F/near/205830364" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/How.20to.20determine.20if.20an.20inst.20has.20.60nuw.60.2F.60nsw.60.20attribute.3F.html#205830364">(Aug 03 2020 at 18:33)</a>:</h4>
<p>corrected output (based on updated run that reflects patch that was posted):</p>
<div class="codehilite"><pre><span></span><code>...
Use:   %5 = add nuw i8 %1, 0 is not an overflowing binop
Use:   %1 = phi i8 [ 0, %0 ], [ %3, %while.cond ] is not an overflowing binop
Use:   %1 = phi i8 [ 0, %0 ], [ %3, %while.cond ] is not an overflowing binop
...
</code></pre></div>



<a name="205830520"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187780-t-compiler/wg-llvm/topic/How%20to%20determine%20if%20an%20inst%20has%20%60nuw%60/%60nsw%60%20attribute%3F/near/205830520" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/How.20to.20determine.20if.20an.20inst.20has.20.60nuw.60.2F.60nsw.60.20attribute.3F.html#205830520">(Aug 03 2020 at 18:34)</a>:</h4>
<p>so my point is: <code>%5 = add nuw i8 %1, 0</code>, is what I <em>want</em> to detect as a (potentially) overflowing binop, so that I can invoke the methods <code>hasNoUnsignedWrap</code> and <code>hasNoSignedWrap</code> methods on it.</p>



<a name="205830562"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187780-t-compiler/wg-llvm/topic/How%20to%20determine%20if%20an%20inst%20has%20%60nuw%60/%60nsw%60%20attribute%3F/near/205830562" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/How.20to.20determine.20if.20an.20inst.20has.20.60nuw.60.2F.60nsw.60.20attribute.3F.html#205830562">(Aug 03 2020 at 18:35)</a>:</h4>
<p>(I already found out the hard way that you cannot just invoke those methods on any old instruction)</p>



<a name="205834668"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187780-t-compiler/wg-llvm/topic/How%20to%20determine%20if%20an%20inst%20has%20%60nuw%60/%60nsw%60%20attribute%3F/near/205834668" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/How.20to.20determine.20if.20an.20inst.20has.20.60nuw.60.2F.60nsw.60.20attribute.3F.html#205834668">(Aug 03 2020 at 19:08)</a>:</h4>
<p>(trying <code>OverflowingBinaryOperator::classof(UserInst)</code> now as an alternative to <code>isa</code>/<code>dyn_cast</code>, since the latter two haven't worked)</p>



<a name="205834907"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187780-t-compiler/wg-llvm/topic/How%20to%20determine%20if%20an%20inst%20has%20%60nuw%60/%60nsw%60%20attribute%3F/near/205834907" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/How.20to.20determine.20if.20an.20inst.20has.20.60nuw.60.2F.60nsw.60.20attribute.3F.html#205834907">(Aug 03 2020 at 19:10)</a>:</h4>
<p>(though from what I've read, in LLVM, <code>isa</code> is just implemented atop <code>classof</code>, so I'm not holding out much hope here.)</p>



<a name="205835359"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187780-t-compiler/wg-llvm/topic/How%20to%20determine%20if%20an%20inst%20has%20%60nuw%60/%60nsw%60%20attribute%3F/near/205835359" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/How.20to.20determine.20if.20an.20inst.20has.20.60nuw.60.2F.60nsw.60.20attribute.3F.html#205835359">(Aug 03 2020 at 19:14)</a>:</h4>
<p>or, oh, maybe I misunderstand the relationship of <del><code>U</code></del> <code>UseI</code> and <code>UserInst</code> (i.e. the meaning of <code>U.getUser()</code>); investigating...</p>



<a name="205837275"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187780-t-compiler/wg-llvm/topic/How%20to%20determine%20if%20an%20inst%20has%20%60nuw%60/%60nsw%60%20attribute%3F/near/205837275" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/187780-t-compiler/wg-llvm/topic/How.20to.20determine.20if.20an.20inst.20has.20.60nuw.60.2F.60nsw.60.20attribute.3F.html#205837275">(Aug 03 2020 at 19:31)</a>:</h4>
<p>Okay yes that was the problem; I was inspecting the flags (and type/operator kind) of the wrong instruction each time</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>